package chapter21.section4;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/1/28 16:37
 */
public class NIOInterruption {
    public static void main(String[] args) throws IOException, InterruptedException {
        ExecutorService service = Executors.newCachedThreadPool();

        ServerSocket serverSocket = new ServerSocket(8681);

        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 8681);
        SocketChannel sc1 = SocketChannel.open(inetSocketAddress);
        SocketChannel sc2 = SocketChannel.open(inetSocketAddress);

        Future<?> future = service.submit(new NIOBlocked(sc1));
        service.execute(new NIOBlocked(sc2));
        service.shutdown();

        TimeUnit.SECONDS.sleep(1);

        future.cancel(true);

        TimeUnit.SECONDS.sleep(1);

        sc2.close();

    }
}
